package Demo;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyFixedThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public MyFixedThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread( () -> {
                while (true) {
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }

    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

class Main {
    public static void main(String[] args) throws InterruptedException {
        MyFixedThreadPool pool = new MyFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int Id = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Task " + Id + " is running");
                }
            });
        }

    }
}
